package com.cn.medicalcare.controller.backstageservlet.product;

import com.cn.medicalcare.customtool.JsonTool;
import com.cn.medicalcare.pojo.*;
import com.cn.medicalcare.service.ProductCategoryService;
import com.cn.medicalcare.service.serviceimpl.ProductCategoryServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

@WebServlet("/backstage/product/getproductcategoryinpage.json")
public class ProductCategoryInPageServlet extends HttpServlet {

    private ProductCategoryService productCategoryService = ProductCategoryServiceImpl.getInstance();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        String describe = req.getParameter("describe");
        String survival = req.getParameter("survival");

        ProductCategory productCategory = new ProductCategory();
        Map<String, Object> map = new LinkedHashMap<>();
        try {
            if (survival != null && survival.length() > 0)
                productCategory.setSurvival(Integer.valueOf(survival));
            productCategory.setName(name).setDescribe(describe);
            int pagenum = Integer.valueOf(req.getParameter("page"));
            int limit = Integer.valueOf(req.getParameter("limit"));
            Page page = null;
            if (pagenum > 0 && limit > 0)
                page = new Page((pagenum - 1) * limit, limit);
            AllPage<ProductCategory> allPage = new AllPage<>(productCategory, page);
            map.put("status", 200);
            map.put("data", productCategoryService.selectProductCategoryInPage(allPage));
            map.put("count", productCategoryService.selectProductCategoryCount(productCategory));
        } catch (Exception e) {
            map.put("status", 500);
        }
        resp.getWriter().print(JsonTool.toJson(map));
    }
}
